package com.example.reactive.router;

import com.example.reactive.handler.RouterHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class ReactiveRouter {

    private static final RequestPredicate ACCEPT_JSON = accept(MediaType.APPLICATION_JSON);

    // Reactive 反应式模型路由配置, 用户路由
    @Bean
    public RouterFunction<ServerResponse> userRoutes(RouterHandler routerHandler) {
        return route()
                .GET("/{user}", ACCEPT_JSON, routerHandler::getUser)
                .POST("/users", ACCEPT_JSON, routerHandler::createUser)
                .PUT("/{user}", ACCEPT_JSON, routerHandler::updateUser)
                .DELETE("/{user}", ACCEPT_JSON, routerHandler::deleteUser)
                .build();
    }

    // Reactive 反应式模型路由配置, 订单路由
    @Bean
    public RouterFunction<ServerResponse> orderRoutes(RouterHandler routerHandler) {
        return route()
                .GET("/{user}", ACCEPT_JSON, routerHandler::getUser)
                .POST("/users", ACCEPT_JSON, routerHandler::createUser)
                .PUT("/{user}", ACCEPT_JSON, routerHandler::updateUser)
                .DELETE("/{user}", ACCEPT_JSON, routerHandler::deleteUser)
                .build();
    }
}

// 配置参考 https://docs.springjava.cn/spring-framework/reference/web/webflux-functional.html
// 关于 Reactive Web 应用的的 Spring Boot 集成更多内容，请查看： https://docs.spring.io/spring-boot/reference/web/reactive.html

// Spring WebFlux 包含 WebFlux.fn，这是一种轻量级函数式编程模型，其中函数用于路由和处理请求，合约则设计为不可变。
// 它是基于注解的编程模型的替代方案，但在其他方面与 Reactive Core 基础相同。